package org.harsh.common;

import cn.hutool.core.codec.Base64;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.Data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Constants {
    public static AttributeKey aesEncryptKey = AttributeKey.valueOf("aesEncryptKey");
    public static AttributeKey encryptKeyValidTime = AttributeKey.valueOf("encryptKeyValidTime");
    public static AttributeKey encryptKeyVersion = AttributeKey.valueOf("encryptKeyVersion");
    public static AttributeKey encryptKeyValid = AttributeKey.valueOf("encryptKeyValid");

    public static final AttributeKey<InetSocketAddress> remoteAddr_key = AttributeKey.valueOf("remoteAddr_key");

    public static final Map<SocketAddress, Channel> peerChannelMap = new ConcurrentHashMap();
    public static final Map<SocketAddress, BigInteger> peerChannelKey = new ConcurrentHashMap();
    public static final Map<Long, RandomAccessFile> transFileInfo = new ConcurrentHashMap();
    public static final Map<Long, Long> transFilePics = new ConcurrentHashMap();

    /**
     * 开始文件传输
     * @param mid
     * @param f
     */
    public synchronized static void transFile(long mid, RandomAccessFile f){
        //文件传输信息
        transFileInfo.put(mid, f);
        //文件碎片
        transFilePics.put(mid, 0l);
    }

    /**
     * 结束文件传输
     * @param mid
     * @throws IOException
     */
    public synchronized static void transOverFile(long mid) throws IOException {
        RandomAccessFile f = getTransFile(mid);
        f.close();
        transFileInfo.remove(mid);
        transFilePics.remove(mid);
    }

    public synchronized static RandomAccessFile getTransFile(long mid){
        if(transFileInfo.containsKey(mid))
            return transFileInfo.get(mid);
        return null;
    }

    public enum ProtocolType {
        TCP,
        UDP,
        UDT,
        KCP
    }

    public enum CastType {
        UNIDIRECTIONAL,
        UNICAST,
        MULTICAST,
        BROADCAST,
        ANYCAST
    }

    /**
     *
     */
    public enum TransType {
        TRANSMSG,
        TRANSPO,
        TRANSFILE;
    }


    public enum MessageType {
        REQHELLO("reqHello", 11),
        ACKHELLO("ackHello", 12),
        REQUPDATEKEY("reqUpdateKey",21),
        ACKUPDATEKEY("ackUpdateKey", 22),
        ACKUPDATEKEYOVER("ackUpdataKeyOver", 23),
        REQPARAMS("reqParams", 31),
        ACKPARAMS("ackParams", 32),
        TRANSMSG("transMsg", 41),
        ACKTRANSMSG("acktransMsg", 42),
        TRANSFILEHEAD("transFileHead", 43),
        TRANSFILEDATA("transFileData", 44),
        TRANSFILEOVER("transFileOver", 45),
        ACKTRANSFILE("ackTransFile", 46),
        ACKTRANSFILEOVER("ackTransFileOver", 47);


        private String msgType;
        private byte code;

        MessageType(String msgType, int code) {
            this.msgType = msgType;
            this.code = (byte) code;
        }

        public String getMsgType() {
            return msgType;
        }

        public byte getCode() {
            return code;
        }


    }
}
